package com.sqznkj.Filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@Component 同时使用会注册两次
@WebFilter(filterName = "loginfilter",urlPatterns = {"/checkUser","/uploadText","/shanchu","/editComInfo","/addCi"})
public class loginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter初始化...");
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest rq = (HttpServletRequest) request;
        HttpServletResponse re = (HttpServletResponse) response;
        String referer = rq.getHeader("referer");
        if (null == referer) {
            System.out.println("没有经过登陆");
            re.sendRedirect("/Wopop.html");
        } else {
            System.out.println("正常登陆");
            chain.doFilter(request, response);
        }
    }
    @Override
    public void destroy() {
        System.out.println("Filter销毁...");
    }
}
